package cloud.maque.admin.impl;

import cloud.maque.admin.domain.entity.MqUser;
import cloud.maque.admin.domain.entity.MqUserLoginThird;
import cloud.maque.admin.mapper.MqUserLoginThirdMapper;
import cloud.maque.admin.service.MqUserLoginThirdService;
import cloud.maque.admin.service.MqUserService;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author 4color
 * @description 针对表【mq_user_login_third】的数据库操作Service实现
 * @createDate 2022-09-26 14:15:41
 */
@Service
public class MqUserLoginThirdServiceImpl extends ServiceImpl<MqUserLoginThirdMapper, MqUserLoginThird> implements MqUserLoginThirdService {


    @Autowired
    MqUserService mqUserService;


    /**
     * 通过经三方
     *
     * @param thirdId
     * @return
     */
    public MqUser GetUser(String thirdUserId, String type, String tenantId) {

        QueryWrapper<MqUserLoginThird> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MqUserLoginThird::getThirdUserId, thirdUserId);
        queryWrapper.lambda().eq(MqUserLoginThird::getThirdType, type);
        queryWrapper.lambda().eq(MqUserLoginThird::getTenantId, tenantId);

        MqUserLoginThird loginThird = this.getOne(queryWrapper);

        if (loginThird == null) {
            return null;
        }

        return mqUserService.getUser(loginThird.getUserId());

    }


    public MqUserLoginThird GetUserThird(MqUserLoginThird loginThird) {

        QueryWrapper<MqUserLoginThird> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MqUserLoginThird::getThirdId, loginThird.getThirdId());
        queryWrapper.lambda().eq(MqUserLoginThird::getThirdType, loginThird.getThirdType());
        queryWrapper.lambda().eq(MqUserLoginThird::getTenantId, loginThird.getTenantId());
        MqUserLoginThird loginThirdNew = this.getOne(queryWrapper);
        return loginThirdNew;
    }

    @Override
    public MqUserLoginThird GetUserThirdByUserId(String userId, String type, String tenantId) {

        QueryWrapper<MqUserLoginThird> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MqUserLoginThird::getUserId, userId);
        queryWrapper.lambda().eq(MqUserLoginThird::getThirdType, type);
        queryWrapper.lambda().eq(MqUserLoginThird::getTenantId, tenantId);
        MqUserLoginThird loginThird = this.getOne(queryWrapper, false);
        return loginThird;
    }


    @Override
    public boolean Bind(MqUserLoginThird loginThird) {

        MqUserLoginThird third = GetUserThird(loginThird);

        if (third == null) {
            loginThird.setLoginId(UUID.fastUUID().toString());
            loginThird.setCreatedAt(new Date());
            return this.saveOrUpdate(loginThird);
        } else {
            return false;
        }
    }

    @Override
    public MqUserLoginThird GetUserByUserAndType(String userId, String type, String tenantId) {
        QueryWrapper<MqUserLoginThird> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MqUserLoginThird::getUserId, userId);
        queryWrapper.lambda().eq(MqUserLoginThird::getThirdType, type);
        queryWrapper.lambda().eq(MqUserLoginThird::getTenantId, tenantId);
        MqUserLoginThird loginThirdNew = this.getOne(queryWrapper);
        return loginThirdNew;
    }


    @Override
    public void CreateThird(MqUserLoginThird loginThird) {

        MqUserLoginThird third = GetUserThird(loginThird);

        if (third == null) {
            UpdateWrapper<MqUserLoginThird> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(MqUserLoginThird::getUserId, loginThird.getUserId());
            third = new MqUserLoginThird();
            third.setLoginId(UUID.fastUUID().toString());
            third.setUserId(loginThird.getUserId());
            third.setThirdId(loginThird.getThirdId());
            third.setThirdUserId(loginThird.getThirdUserId());
            third.setThirdType(loginThird.getThirdType());
            third.setTenantId(loginThird.getTenantId());
            third.setThirdName(loginThird.getThirdName());
            third.setCreatedAt(new Date());
            this.saveOrUpdate(third, updateWrapper);
        }
    }


}




